package com.xnew.service.impl;

import com.xnewtech.xplatform.swan.libs.mp.page.PageFactory;
import com.xnewtech.xplatform.swan.model.page.PageResult;
import com.xnew.entity.SysFileInfo;
import com.xnew.mapper.SysFileInfoMapper;
import com.xnew.model.params.SysFileInfoParam;
import com.xnew.model.result.SysFileInfoResult;
import  com.xnew.service.SysFileInfoService;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 文件信息表 服务实现类
 * </p>
 *
 * @author shj
 * @since 2021-08-09
 */
@Service
public class SysFileInfoServiceImpl extends ServiceImpl<SysFileInfoMapper, SysFileInfo> implements SysFileInfoService {

    @Override
    public void add(SysFileInfoParam param){
        SysFileInfo entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(SysFileInfoParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(SysFileInfoParam param){
        SysFileInfo oldEntity = getOldEntity(param);
        SysFileInfo newEntity = getEntity(param);
        BeanUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public SysFileInfoResult findBySpec(SysFileInfoParam param){
        return null;
    }

    @Override
    public List<SysFileInfoResult> findListBySpec(SysFileInfoParam param){
        return null;
    }

    @Override
    public PageResult<SysFileInfoResult> findPageBySpec(SysFileInfoParam param){
        Page pageContext = getPageContext();
        IPage<SysFileInfoResult> page = this.baseMapper.customPageList(pageContext, param);
        return new PageResult<>(page);
    }

    private Serializable getKey(SysFileInfoParam param){
        return param.getFileId();
    }

    private Page getPageContext() {
        return PageFactory.defaultPage();
    }

    private SysFileInfo getOldEntity(SysFileInfoParam param) {
        return this.getById(getKey(param));
    }

    private SysFileInfo getEntity(SysFileInfoParam param) {
        SysFileInfo entity = new SysFileInfo();
        BeanUtil.copyProperties(param, entity);
        return entity;
    }

}
